package com.stylefeng.guns.modular.channel.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.modular.channel.service.*;
import com.stylefeng.guns.modular.system.model.ChannelSuper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.Channel;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 子渠道控制器
 *
 * @author fengshuonan
 * @Date 2018-08-26 12:09:13
 */
@Controller
@RequestMapping("/channel")
public class ChannelController extends BaseController {

    private String PREFIX = "/channel/channel/";

    @Autowired
    private IChannelService channelService;

    @Autowired
    private IAppDeviceService deviceService;

    @Autowired
    private IAppUserService appUserService;

    @Autowired
    private ILoanOrderService loanOrderService;

    private Long channelId;



    /**
     * 跳转到子渠道首页
     */
    @RequestMapping("")
    public String index(Long channelId) {
        this.channelId = channelId;
        return PREFIX + "channel.html";
    }

    /**
     * 跳转到添加子渠道
     */
    @RequestMapping("/channel_add")
    public String channelAdd() {
        return PREFIX + "channel_add.html";
    }

    /**
     * 跳转到修改子渠道
     */
    @RequestMapping("/channel_update/{channelId}")
    public String channelUpdate(@PathVariable Integer channelId, Model model) {
        Channel channel = channelService.selectById(channelId);
        model.addAttribute("item",channel);
        LogObjectHolder.me().set(channel);
        return PREFIX + "channel_edit.html";
    }

    /**
     * 获取子渠道列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String channelName) {

        Date beginDate = null;
        Date endDate = null;

        EntityWrapper<Channel> channelEntityWrapper = new EntityWrapper<>();
        channelEntityWrapper.where(channelId != null, "super_channel_id={0}", channelId);
        channelEntityWrapper.like("channel_name", channelName);
        List<Map<String, Object>> mapList = channelService.selectMaps(channelEntityWrapper);

        int totalActiveCount = 0, totalRegisterCount = 0, totalRequestCount = 0, totalBargainCount = 0;
        BigDecimal totalBargainAmount = new BigDecimal(0.00);

        for (Map<String, Object> channelMap: mapList) {

            Long channelId = (Long) channelMap.get("id");

            EntityWrapper wrapper = new EntityWrapper();
            wrapper.between(beginDate != null && endDate != null,"create_time", beginDate, endDate);//设置查询时间范围
            wrapper.where("channel_id={0}", channelId);
            int activeCount = deviceService.selectCount(wrapper);
            int registerCount = appUserService.selectCount(wrapper);

            int requestCount = loanOrderService.selectCountRequest(channelId, beginDate, endDate);
            int bargainCount = loanOrderService.selectCountBargain(channelId, beginDate, endDate);
            BigDecimal bargainAmount = loanOrderService.selectCountBargainAmount(channelId, beginDate, endDate);

            channelMap.put("status", StatusEnum.desc((Integer) channelMap.get("isEnabled")));
            channelMap.put("activeCount", activeCount);
            channelMap.put("registerCount", registerCount);
            channelMap.put("requestCount", requestCount);
            channelMap.put("bargainCount", bargainCount);
            channelMap.put("bargainAmount", bargainAmount);

            totalActiveCount = totalActiveCount + activeCount;
            totalRegisterCount = totalRegisterCount + registerCount;
            totalRequestCount = totalRequestCount + requestCount;
            totalBargainCount = totalBargainCount + bargainCount;
            totalBargainAmount =  totalBargainAmount.add(bargainAmount);

        }
        HashMap<String, Object> countMap = new HashMap<>();
        countMap.put("activeCount", totalActiveCount);
        countMap.put("registerCount", totalRegisterCount);
        countMap.put("requestCount", totalRequestCount);
        countMap.put("bargainCount", totalBargainCount);
        countMap.put("bargainAmount", totalBargainAmount);
        mapList.add(0, countMap);
        return mapList;
    }

    /**
     * 新增子渠道
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Channel channel) {
        channel.setCreateTime(new Date());
        channelService.insert(channel);
        return SUCCESS_TIP;
    }

    /**
     * 删除子渠道
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer channelId) {
        channelService.deleteById(Long.valueOf(channelId));
        return SUCCESS_TIP;
    }

    /**
     * 修改子渠道
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Channel channel) {
        channelService.updateById(channel);
        return SUCCESS_TIP;
    }

    /**
     * 子渠道详情
     */
    @RequestMapping(value = "/detail/{channelId}")
    @ResponseBody
    public Object detail(@PathVariable("channelId") Integer channelId) {
        return channelService.selectById(channelId);
    }
}
